package cn.shyouchi.wuguanjia.android.ui.event.proprietor;

import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.widget.dialog.TimePickerDialogFragment;

/**
 * Created by movier on 9/21/16.
 */

public class ArrangeTimeSetDialog extends DialogFragment {

    @BindView(R.id.advance_edit_dialog_title)
    TextView title;

    @BindView(R.id.advance_edit_dialog_edit_text_1)
    TextView editText1;
    @BindView(R.id.advance_edit_dialog_edit_text_2)
    TextView editText2;

    private static final String TAG = "AdvanceEditDialog";

    private static final String ARG_TITLE = "title";
    private static final String ARG_SUBTITLE = "subtitle";
    private static final String ARG_HINT_ONE = "hint1";
    private static final String ARG_HINT_TWO = "hint2";


    public interface ArrangeTimeSetDialogListener {
        void onPositiveClick(DialogFragment dialog, String content);
    }

    ArrangeTimeSetDialogListener mListener;


    public void setClickListener(ArrangeTimeSetDialogListener mListener) {
        this.mListener = mListener;
    }


    public static ArrangeTimeSetDialog newInstance() {
        ArrangeTimeSetDialog fragment = new ArrangeTimeSetDialog();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.dialog_arrange_set_time, container, false);
        ButterKnife.bind(this, view);


        editText1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showTimePicker((TextView) v, "选择上班时间");
            }
        });
        editText2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showTimePicker((TextView) v, "选择下班时间");
            }
        });

        return view;
    }





    @OnClick(R.id.advance_edit_dialog_confirm_button)
    void confirmButtonTapped() {
        if (editText1.getText().toString().isEmpty()) {
            Toast.makeText(getActivity(), "上班时间不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        if (editText2.getText().toString().isEmpty()) {
            Toast.makeText(getActivity(), "下班时间不能为空", Toast.LENGTH_SHORT).show();
            return;
        }


        if (mListener != null) {
            mListener.onPositiveClick(this, String.format("%1s-%2s", editText1.getText().toString(), editText2.getText().toString()));
        }
        getDialog().cancel();
    }


    public void showTimePicker(final TextView tv, String title) {
        TimePickerDialogFragment mTimePickerDialog;
        mTimePickerDialog = new TimePickerDialogFragment(((BaseActivity) getActivity()).getSupportFragmentManager(), title, "");
        mTimePickerDialog.setOnTimePickedListener(new TimePickerDialogFragment.onTimePickedListener() {
            @Override
            public void onPicked(String time) {
                tv.setText(time);
                tv.setTag(1);
            }
        });
        mTimePickerDialog.showDialog();
    }

    @OnClick(R.id.advance_edit_dialog_cancel_button)
    void cancelButtonTapped() {
        getDialog().cancel();
    }
}
